/*
 * Package il.ac.biu.cs.grossmm.impl.keys
 * File PatternMatcherTest.java
 * Created on Sep 5, 2006
 *
 */
package il.ac.biu.cs.grossmm.junit_api_tests.keys;

import java.io.Serializable;

import org.junit.Test;
import static org.junit.Assert.*;

import static il.ac.biu.cs.grossmm.impl.keys.PatternMatcher.*;
import static il.ac.biu.cs.grossmm.api.presence.BaseVocabulary.*;
import static il.ac.biu.cs.grossmm.api.keys.KeyFactory.*;
import static il.ac.biu.cs.grossmm.api.keys.KeyPattern.*;
import static il.ac.biu.cs.grossmm.api.keys.PatternFactory.*;



public class PatternKeyMatchTest {
    
    @Test
    public void match1() {
        assertTrue(
                matches(
                        pattern(),
                        key()
                        )
                );
    }
    
    @Test
    public void match2() {
        assertTrue(
                matches(
                        ANY_KEY,
                        key("Presence")
                        )
                );
    }
    
    @Test
    public void match3() {
        assertTrue(
                matches(
                        maskPattern(Object.class),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
    }
    
    @Test
    public void match4() {
        assertTrue(
                matches(
                        ANY_SIMPLE_KEY,
                        key("Presence")
                        )
                );
    }
    
    @Test
    public void match5() {
        assertFalse(
                matches(
                        ANY_SIMPLE_KEY,
                        key("Presence", entry(CONTENT_TYPE))
                        )
                );
    }
        
    @Test
    public void match6() {
        assertFalse(
                matches(
                        ANY_SIMPLE_KEY,
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
    }
    
    @Test
    public void match7() {
        assertTrue(
                matches(
                        pattern("Presence"),
                        key("Presence")
                        )
                );
        
        assertFalse(
                matches(
                        pattern("Presence"),
                        key("Bar")
                        )
                );
    }
    
    @Test
    public void match8() {
        assertFalse(
                matches(
                        pattern("Presence"),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
    }
    
    @Test
    public void match9() {
        assertTrue(
                matches(
                        maskPattern("Presence"),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
    }
    
    @Test
    public void match10() {
        assertTrue("A",
                matches(
                        pattern("Presence", mandatory(CONTENT_TYPE, Integer.class), mandatory(RESOURCE_TYPE)),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
        
        assertTrue("B",
                matches(
                        pattern("Presence", mandatory(CONTENT_TYPE, 123), optional(RESOURCE_TYPE)),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
        
        assertTrue("C", 
                matches(
                        pattern("Presence", optional(CONTENT_TYPE, ANY_SIMPLE_KEY), optional(RESOURCE_TYPE)),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
    }
    
    @Test
    public void match11() {
        assertFalse(
                matches(
                        pattern("Presence", mandatory(CONTENT_TYPE, 123), mandatory(WATCHER)),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
        
        assertFalse(
                matches(
                        pattern("Presence", mandatory(CONTENT_TYPE, Object.class)),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
        
        assertTrue(
                matches(
                        pattern("Presence", optional(WATCHER), optional(CONTENT_TYPE, Integer.class)),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
        
        assertFalse(
                matches(
                        pattern("Presence", optional(CONTENT_TYPE, ANY_KEY), optional(RESOURCE_TYPE)),
                        key("Bar", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
    }
    
    @Test
    public void match12() {
        assertTrue(
                matches(
                        maskPattern("Presence", mandatory(RESOURCE_TYPE), mandatory(CONTENT_TYPE)),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE))
                        )
                );
        
        assertTrue(
                matches(
                        maskPattern("Presence", mandatory(CONTENT_TYPE, Void.class)),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, null))
                        )
                );
        
        assertTrue(
                matches(
                        maskPattern("Presence", optional(CONTENT_TYPE, Integer.class)),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
    }
    
    @Test
    public void match13() {
        assertFalse(
                matches(
                        maskPattern("Bar", mandatory(CONTENT_TYPE), mandatory(WATCHER)),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
        
        assertFalse(
                matches(
                        maskPattern("Presence", mandatory(WATCHER)),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
        
        assertTrue(
                matches(
                        maskPattern("Presence", optional(WATCHER)),
                        key("Presence", entry(RESOURCE_TYPE), entry(CONTENT_TYPE, 123))
                        )
                );
    }
    
    @Test
    public void match14() {
        assertTrue(
                matches(
                        pattern(String.class),
                        key("Presence")
                        )
                );
        
        assertTrue(
                matches(
                        pattern(String.class),
                        key((Serializable) null)
                        )
                );
        
        assertFalse(
                matches(
                        pattern(String.class),
                        key(123)
                        )
                );
    }
    
    @Test
    public void match15() {
        assertTrue(
                matches(
                        pattern(String.class, mandatory(WATCHER, Integer.class)),
                        key("Presence", entry(WATCHER, 123))
                        )
                );
        
        assertTrue(
                matches(
                        pattern(String.class, mandatory(WATCHER, Object.class)),
                        key("Presence", entry(WATCHER, 123))
                        )
                );
        
        assertFalse(
                matches(
                        pattern(String.class, mandatory(WATCHER, Integer.class)),
                        key("Presence", entry(WATCHER, "Bar"))
                        )
                );
        
        assertFalse(
                matches(
                        pattern(String.class, optional(WATCHER, Integer.class)),
                        key("Presence", entry(WATCHER, "Bar"))
                        )
                );
    }
    
    @Test
    public void match16() {
        assertTrue(
                matches(
                        pattern(String.class, mandatory(WATCHER, 123)),
                        key("Presence", entry(WATCHER, 123))
                        )
                );
        
        assertFalse(
                matches(
                        pattern(String.class, mandatory(WATCHER, 123)),
                        key("Presence", entry(WATCHER, 1))
                        )
                );
        
        assertFalse(
                matches(
                        pattern(String.class, optional(WATCHER, 123)),
                        key("Presence", entry(WATCHER, 1))
                        )
                );
    }
}
